package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.getData.GetData;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class FFP_viewpager_adapter extends PagerAdapter{
    private Context context;
    private String[] titles;
    private ArrayList<View> views;  //每个选项卡对应的布局

    String up_id;

    FFP_recyleview_adapter adapter_follows;
    FFP_recyleview_adapter adapter_fans;



    public FFP_viewpager_adapter(String[] titles, ArrayList<View> views, Context context,String up_id){
        this.titles=titles;
        this.views=views;
        this.context=context;
        this.up_id=up_id;
        adapter_follows=new FFP_recyleview_adapter(context,10,true,up_id);
        adapter_fans=new FFP_recyleview_adapter(context,10,false,up_id);
    }

    //实例化选项卡
    @NotNull
    @SuppressLint("ShowToast")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View v = views.get(position);
        container.addView(v);

        RecyclerView recyclerView=v.findViewById(R.id.users_viewpager_page2_recycleview);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        EditText searchtxt=v.findViewById(R.id.searchtxt);
        //关注
        if (position==0){
            recyclerView.setAdapter(adapter_follows);
            GetData.getFollowOrFansUser(context,adapter_follows,true,up_id);
            //搜索功能
            v.findViewById(R.id.searchico).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (searchtxt.getText().toString().length()>0){
                        GetData.searchfanorfollow(context,searchtxt.getText().toString(),true,adapter_follows);
                    }else
                        GetData.getFollowOrFansUser(context,adapter_follows,true,up_id);
                }
            });
            adapter_follows.setOther_adapter(adapter_fans);
        }
        else {
            recyclerView.setAdapter(adapter_fans);
            GetData.getFollowOrFansUser(context,adapter_fans,false,up_id);
            //搜索功能
            v.findViewById(R.id.searchico).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (searchtxt.getText().toString().length()>0){
                        GetData.searchfanorfollow(context,searchtxt.getText().toString(),false,adapter_fans);
                    }else
                        GetData.getFollowOrFansUser(context,adapter_fans,false,up_id);
                }
            });
            adapter_fans.setOther_adapter(adapter_follows);
        }
        return v;
    }

    //删除选项卡
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }
    //返回选项大小
    @Override
    public int getCount() {
        return 2;
    }

    //判断是否是为返回选项卡
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    //获取标题
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}
